// Copyright (c) Microsoft Corporation. All rights reserved.

function OnFinish(selProj, selObj)
{
	try
	{
		var strProjectPath = wizard.FindSymbol("PROJECT_PATH");
		var strProjectName = wizard.FindSymbol("PROJECT_NAME");

		var bEmptyProject = wizard.FindSymbol("EMPTY_PROJECT");

		wizard.AddSymbol("RC_FILE_NAME",CreateSafeRCFileName(strProjectName) + ".rc");

		selProj = CreateProject(strProjectName, strProjectPath);
		selProj.Object.Keyword = "Win32Proj";

		AddCommonConfig(selProj, strProjectName);
		var strAppType = GetAppType();
		AddSpecificConfig(selProj, strProjectName, bEmptyProject, strAppType);

		SetupFilters(selProj);

		if (!bEmptyProject)
		{
			SetResDlgFont();
			
			var Pch = wizard.FindSymbol("PRE_COMPILED_HEADER");
			if (!Pch) 
			{
				AddFilesToProjectWithInfFile(selProj, strProjectName);
				SetNoPchSettings(selProj);
			}
			else
			{
				AddFilesToProjectWithInfFile(selProj, strProjectName);
				SetCommonPchSettings(selProj);	
			}
		}
		selProj.Object.Save();
	}
	catch(e)
	{
		if (e.description.length != 0)
			SetErrorInfo(e);
		return e.number
	}
}

function SetFileProperties(projfile, strName)
{
	if (strName == "dllmain.cpp") {
		var Configs = projfile.Object.FileConfigurations;
		for(var i=1;i<=Configs.Count;++i) {
			var Config = Configs(i);
			var CLTool = Config.Tool;
			CLTool.CompileAsManaged = 0; // Force no /CLR
			CLTool.UsePrecompiledHeader = pchNone; // No pre-compiled headers			
		}
	}
}

function GetTargetName(strName, strProjectName, strResPath, strHelpPath)
{
	try
	{
		var strTarget = strName;

		if (strName == "readme.txt") {
			strTarget = "readme.txt";
		} else if (strName == "resource.h") {
			strTarget = "resource.h";
		} else if (strName == "small.ico") {
			strTarget = "small.ico";
		} else if (strName == "root.rc") {
			strTarget = wizard.FindSymbol("RC_FILE_NAME");
		} else if (strName.substr(0, 4) == "root") {
			strTarget = strProjectName + strName.substr(4);
		}
		return strTarget; 
	}
	catch(e)
	{
		throw e;
	}
}

function GetAppType()
{
	try
	{
		var strAppType = "";
		if (wizard.FindSymbol("CONSOLE_APP"))
			strAppType = "CONSOLE";
		else
		{
			if (wizard.FindSymbol("WIN_APP"))
				strAppType = "WIN";
			else
			{
				if (wizard.FindSymbol("DLL_APP"))
					strAppType = "DLL";
				else
				{
					if (wizard.FindSymbol("LIB_APP"))
						strAppType = "LIB";
				}
			}
		}
		return strAppType;
	}
	catch(e)
	{
		throw e;
	}
}

function AddSpecificConfig(proj, strProjectName, bEmptyProject, strAppType)
{
	try
	{
		var bMFC = wizard.FindSymbol("SUPPORT_MFC");
		var bATL = wizard.FindSymbol("SUPPORT_ATL");
        
		var config = proj.Object.Configurations("Debug");
		config.IntermediateDirectory = "build\\debug\\"; 		// $(Configuration)\
		config.OutputDirectory = "$(SolutionDir)\\debug\\"; 	// $(SolutionDir)$(Configuration)\
		config.DebugSettings.WorkingDirectory = "$(OutDir)";	// $(ProjectDir)
		
		var unicode = wizard.FindSymbol("USE_UNICODE");
		if(unicode)
			config.CharacterSet = charSetUNICODE;
		else
			config.CharacterSet = charSetNOTSET;
			
		if (strAppType == "LIB")
			config.ConfigurationType = typeStaticLibrary;
		else if (strAppType == "DLL")
			config.ConfigurationType = typeDynamicLibrary;

		var CLTool = config.Tools("VCCLCompilerTool");
		// CLTool.PrecompiledHeaderFile = "$(OutDir)" + strProjectName + ".pch";
		
		// if(!wizard.FindSymbol("PRE_COMPILED_HEADER"))
		// {
			// CLTool.UsePrecompiledHeader = pchNone;
		// }

		// CLTool.RuntimeLibrary = rtMultiThreadedDebugDLL;

		var strDefines = CLTool.PreprocessorDefinitions;
		if (strDefines != "") strDefines += ";";
		strDefines += GetPlatformDefine(config);
		strDefines += "_DEBUG";

		switch(strAppType)
		{
			case "CONSOLE":
				strDefines += ";_CONSOLE";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				if (bATL)
					config.UseOfATL = useATLStatic;
				break;
			case "LIB":
				strDefines += ";_LIB";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				break;
			case "DLL":
				strDefines += ";_WINDOWS;_USRDLL;";
				var strExports = wizard.FindSymbol("UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME") + "_EXPORTS";
				strDefines += strExports;
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				if (bATL)
					config.UseOfATL = useATLStatic;
				break;
			case "WIN":
				strDefines += ";_WINDOWS";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				if (bATL)
					config.UseOfATL = useATLStatic;
				break;
			default:
				break;
		}

		CLTool.PreprocessorDefinitions = strDefines;
		if (bEmptyProject)
			CLTool.UsePrecompiledHeader = pchNone;

		if (strAppType != "LIB")
		{
			var LinkTool = config.Tools("VCLinkerTool");
			LinkTool.GenerateDebugInformation = true;
			LinkTool.LinkIncremental = linkIncrementalYes;

			if (strAppType == "DLL" || strAppType == "WIN")
				LinkTool.SubSystem = subSystemWindows;
			else
				LinkTool.SubSystem = subSystemConsole;
			
			/*if(wizard.FindSymbol("NO_DEFAULT_LIB"))
			{
				LinkTool.IgnoreAllDefaultLibraries = true;
				CLTool.BufferSecurityCheck = false;
				var w = (unicode)?"w":"";
				switch(strAppType)
				{
					case "CONSOLE":
						LinkTool.EntryPointSymbol = w+"main";
						break;
					case "DLL":
						LinkTool.EntryPointSymbol = w+"DllMain";
						break;
					case "WIN":
						LinkTool.EntryPointSymbol = w+"WinMain";
						break;
					default:
						break;
				}
			}*/
			
			if(wizard.FindSymbol("NO_MANIFEST"))
			{
				LinkTool.GenerateManifest = false;
				// var ManifestTool = config.Tools("VCManifestTool");
				// ManifestTool.EmbedManifest = false;
			}
		}

		config = proj.Object.Configurations.Item("Release");
		config.IntermediateDirectory = "build\\release\\";		// $(Configuration)\
		config.OutputDirectory = "$(SolutionDir)\\release\\";	// $(SolutionDir)$(Configuration)\
		config.DebugSettings.WorkingDirectory = "$(OutDir)";	// $(ProjectDir)

		var unicode = wizard.FindSymbol("USE_UNICODE");
		if(unicode)
			config.CharacterSet = charSetUNICODE;
		else
			config.CharacterSet = charSetNOTSET;

		if (strAppType == "LIB")
			config.ConfigurationType = typeStaticLibrary;
		else if (strAppType == "DLL")
			config.ConfigurationType = typeDynamicLibrary;

		var CLTool = config.Tools("VCCLCompilerTool");

		// if(!wizard.FindSymbol("PRE_COMPILED_HEADER"))
		// {
			// CLTool.UsePrecompiledHeader = pchNone;
		// }

		CLTool.RuntimeLibrary = rtMultiThreaded;

		var strDefines = CLTool.PreprocessorDefinitions;
		if (strDefines != "") strDefines += ";";
		strDefines += GetPlatformDefine(config);
		strDefines += "NDEBUG";
		if (bEmptyProject)
			CLTool.UsePrecompiledHeader = pchNone;

		switch(strAppType)
		{
			case "CONSOLE":
				strDefines += ";_CONSOLE";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				if (bATL)
					config.UseOfATL = useATLStatic;
				break;
			case "LIB":
				strDefines += ";_LIB";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				break;
			case "DLL":
				strDefines += ";_WINDOWS;_USRDLL;";
				var strExports = wizard.FindSymbol("UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME") + "_EXPORTS";
				strDefines += strExports;
				break;
			case "WIN":
				strDefines += ";_WINDOWS";
				if (bMFC)
					config.UseOfMFC = useMfcDynamic;
				if (bATL)
					config.UseOfATL = useATLStatic;
				break;
			default:
				break;
		}

		CLTool.PreprocessorDefinitions = strDefines;
		
		if (strAppType != "LIB")
		{
			var LinkTool = config.Tools("VCLinkerTool");
			LinkTool.GenerateDebugInformation = false;
			LinkTool.LinkIncremental = linkIncrementalNo;

			if (strAppType == "DLL" || strAppType == "WIN")
				LinkTool.SubSystem = subSystemWindows;
			else
				LinkTool.SubSystem = subSystemConsole;
			
			if(wizard.FindSymbol("NO_DEFAULT_LIB"))
			{
				LinkTool.IgnoreAllDefaultLibraries = true;
				CLTool.BufferSecurityCheck = false;
				var w = (unicode)?"w":"";
				switch(strAppType)
				{
					case "CONSOLE":
						LinkTool.EntryPointSymbol = w+"main";
						break;
					case "DLL":
						LinkTool.EntryPointSymbol = w+"DllMain";
						break;
					case "WIN":
						LinkTool.EntryPointSymbol = w+"WinMain";
						break;
					default:
						break;
				}
			}
			
			if(wizard.FindSymbol("NO_MANIFEST"))
			{
				LinkTool.GenerateManifest = false;
				// var ManifestTool = config.Tools("VCManifestTool");
				// ManifestTool.EmbedManifest = false;
			}
		}
	}
	catch(e)
	{
		throw e;
	}
}